<?php
/**
 * Created by PhpStorm.
 * User: 86131
 * Date: 2020/10/19
 * Time: 18:35
 */

namespace apiadmin\modules\v1\controllers\finance;
use apiadmin\modules\models\admin\User;
use apiadmin\modules\models\finance\BalanceOperate;
use common\models\Balance;
use common\models\ExchangeLog;
use common\models\StaticInstance;
use \Yii;
use apiadmin\modules\v1\controllers\CoreController;
use common\models\finance\StaticInstanceModel;
use common\models\finance\StaticInstanceOrderModel;
use common\utils\OutputExecl;
/*
	兑换相关控制器
*/
class ExchangeController extends CoreController
{

    //test
    public function actionTest()
    {
        $this->out('Test');
    }

    /*
		*兑换列表
	*/
    public function actionExchangeList()
    {

        $where  = $this->formartWhere();
        $params = array(
            'field'	=> ['*'],
            'order' => 'create_time desc',
            'page'	=> $this->request('page','1'),
            'limit' => $this->request('page_size',10),
        );
        $list = ExchangeLog::dataList($where,$params);
        $pages = ExchangeLog::$pages;
        $typeName = Balance::balanceTypeName();
        $this->out('兑换列表',$list,array('pages'=>$pages, 'type_name' => $typeName));
    }

    //组装条件
    public function formartWhere()
    {
        $where = [];
        $whereAnd = [];
        $searchKeys = json_decode($this->request('search'),1);

        if(!$searchKeys) return array('where'=>$where,'whereAnd'=>$whereAnd);
        foreach($searchKeys as $k=>$val)
        {
            if(!$val){
                if($k=='state' && ''.$val=='0'){
                    $where[$k] = $val;
                }
                continue;
            }
            if($k=='date')
            {
                if(!$val['0'] || !$val['1']) continue;
                $whereAnd[] = ['between', 'create_time', strtotime($val[0]),strtotime($val[1])];
            }else
            {
                $where[$k] = $val;
            }
        }

        return array('where'=>$where,'whereAnd'=>$whereAnd);
    }

    /*
		获取单个兑换信息
		* id 兑换ID
	*/
    public function actionExchangeInfo()
    {
        if(!$id = $this->request('id')) $this->error('参数错误');
        $field  = ['*'];
        $info = ExchangeLog::getOne(['id'=>$id]);
        $typeName = Balance::balanceTypeName();
        $this->out('会员信息',$info, array('type_name' => $typeName));
    }

    //兑换状态修改
    public function actionExchangeEdit()
    {
        $params = $this->request;

        $auth_key = $this->request('auth_key');
        $user_model = User::find()->where(['auth_key'=> $auth_key])->asarray()->one();
        if(!$user_model){
            if(!$user_model) return $this->error('参数有误');
        }
        if(!$id = $this->request('id')) $this->error('参数错误');
        $state = intval($this->request('state'))+0;
        if(!$this->request('state'))
            return $this->error('参数错误');
        if($user_model['password']!=md5($this->request('loginpwd'))) return $this->error('密码错误');

        $model = ExchangeLog::find()->where(['id'=>$id])->one();
        if(intval($model->state)!=2){
            return $this->error('不允许修改');
        }
        $model->state=$state;
        if(intval($state)!=1&&intval($state)!=3){
            return $this->error('不允许修改');
        }
        try{
            $transaction = Yii::$app->db->beginTransaction();
            if(!$model->save(false)) $this->error('修改失败');
            //增加余额
            if($state==3){
                $re = Balance::addBalance($model->member_id,1,$model->to_money,13,$model->id, '兑换驳回');
            }else{
                $re = Balance::addBalance($model->member_id,2,$model->to_money,13,$model->id, '兑换');
            }

            if (isset($re['state']) && $re['state']==false) throw new \Exception($re['msg']);
            $transaction->commit();
            $this->out('修改成功');
        }catch (\Exception $e){
            $transaction->rollBack();
            return $this->error('系统繁忙,请稍后重试');
        }

    }
}